/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.job.task;

import com.itheima.health.common.RedisConst;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * <dl>
 * <dd>描述: ~ CleanTask</dd>
 * <dd>创建时间：  9:40 2021/1/9</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/1/9      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Component
@Slf4j
public class CleanTask {

    private final static Long MAX_TIME = 30*1000L; //最大允许存放时间30秒

    @Autowired
    private RedisTemplate redisTemplate;

    @DubboReference
    private SetmealService setmealService;

    @Autowired
    private QiniuUtils qiniuUtils;

    @Scheduled(cron = "0/5 * * * * ?")
    public void cleanImage(){
        log.info("定时任务开始执行");
        //1.从redis获取图片名称---set集合
        Set<String> members = redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).members();
        //2.遍历截取时间戳
        for (String fileName : members) {
            String[] split = fileName.split("_");
            //存入redis中时的时间戳
            Long createTime = Long.valueOf(split[0]);
            //3.当前时间-时间戳 与最大允许时间进行判断
            if((System.currentTimeMillis() - createTime)< MAX_TIME){
                //4.如果小于最大允许时间 放行
                log.info("[文件没有超时忽略]fileName: {}", fileName);
                continue;
            }
            //5.大于最大允许时间 判断是否有被使用
            int count = setmealService.count(fileName);
            if(count == 0){
                //6.如果没有被使用则删除---》七牛云删除
                qiniuUtils.deleteFileFromQiniu(fileName);
                log.info("[文件没有被使用，从七牛云删除]fileName: {}", fileName);
            }
            //7.从redis删除
            redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).remove(fileName);
            log.info("[文件存放在redis中超时，从redis删除]fileName: {}", fileName);
        }
    }
}
